package org.deri.vor.map;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.deri.vor.R;
import org.deri.vor.mobilesensing.globalsensor.ListPropertyAdapter;
import org.deri.vor.mobilesensing.globalsensor.Property;
import org.deri.vor.mobilesensing.globalsensor.SensorXMLParser;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextPaint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;



public class SensorOverLay extends ItemizedOverlay<OverlayItem> {
	String url="";
	Dialog dialog;
	
	private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

	private static Activity activity;

	public static void setAct(Activity act) {
		activity = act;
	}

	public SensorOverLay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		markerHeight = ((BitmapDrawable) defaultMarker).getBitmap().getHeight();
	}

	public SensorOverLay(Drawable defaultMarker, String url) {
		this(defaultMarker);
		this.url = url;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mapOverlays.get(i);
	}

	@Override
	public int size() {
		return mapOverlays.size();
	}

	
	ListView  propertyListlv;
	ListPropertyAdapter propertyAdapter;
	SensorXMLParser sXP;
	String ImageURL;
	
	public ArrayList <Property> propertiesList;
	Property prop;	
	
	@Override
	protected boolean onTap(int index) {
		String title	=	mapOverlays.get(index).getTitle();
		dialog	=	new Dialog(activity);
		dialog.setTitle(title);
		
		if (!url.contains("http://")){
			dialog.setContentView(R.layout.chuabiet);
			dialog.setTitle("Coordinate");
			propertyListlv	=	(ListView)dialog.findViewById(R.id.SensorListViewId);
			propertyListlv.setClickable(false);
			propertiesList = new ArrayList<Property>();
			
			prop	=	new Property("Latitude : ", Integer.toString(mapOverlays.get(index).getPoint().getLatitudeE6()));
			propertiesList.add(prop);
			
			prop	=	new Property("Longitude: ", Integer.toString(mapOverlays.get(index).getPoint().getLongitudeE6()));
			propertiesList.add(prop);
			
			propertyAdapter = 	new ListPropertyAdapter(activity, propertiesList);
			propertyListlv.setAdapter(propertyAdapter);
		
		}else{
			if (title.equals("Traffic")){
				dialog.setContentView(R.layout.image);
				ImageView iv	=	(ImageView)dialog.findViewById(R.id.photo);
				
				sXP	=	new SensorXMLParser(url);
				for (int i = 0; i<sXP.propertiesList.size();i++){
					if (sXP.propertiesList.get(i).getName().equals("Picture:  ")){
						ImageURL = sXP.propertiesList.get(i).getValue();
					}
				}try {
					Drawable a = Drawable.createFromStream((InputStream) 
							new URL(ImageURL).getContent(),"photo");
					if (a!=null) iv.setImageDrawable(a);
				} catch (MalformedURLException e) {e.printStackTrace();
				} catch (IOException e) {e.printStackTrace();}
		
			}else if(title.equals("Webcam")){
				try {
					dialog.setContentView(R.layout.image);
					ImageView iv	=	(ImageView)dialog.findViewById(R.id.photo);
					
					Drawable a = Drawable.createFromStream((InputStream) new URL(url).getContent(),"photo");
						if (a!=null) iv.setImageDrawable(a);
				
				} catch (MalformedURLException e) {e.printStackTrace();
				} catch (IOException e) {e.printStackTrace();}
			
			}else{
				dialog.setContentView(R.layout.chuabiet);
				sXP	=	new SensorXMLParser(url);
				propertyListlv	=	(ListView)dialog.findViewById(R.id.SensorListViewId);
				propertyListlv.setClickable(false);
				propertyAdapter = 	new ListPropertyAdapter(activity, sXP.propertiesList);
				propertyListlv.setAdapter(propertyAdapter);
			
			}
		}
        
        Button	ok	=	(Button)dialog.findViewById(R.id.ok);
		ok.setOnClickListener(new OnClickListener(){
			@Override
				public void onClick(View arg0) {
				dialog.dismiss();
			}
		} );
		
		
		dialog.show();
		return true;
		
	}

	public void addOverlay(OverlayItem overlay) {
		mapOverlays.add(overlay);
		this.populate();
	}

	private static final int FONT_SIZE = 12;
	private static final int TITLE_MARGIN = 3;
	private int markerHeight = -15;
	
	@Override
	public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);

		for (OverlayItem item : mapOverlays) {
			if(item.getTitle().length()>1)
			{
			GeoPoint point = item.getPoint();
			Point markerBottomCenterCoords = new Point();
			mapView.getProjection().toPixels(point, markerBottomCenterCoords);


			TextPaint paintText = new TextPaint();
			Paint paintRect = new Paint();

			Rect rect = new Rect();
			paintText.setTextSize(FONT_SIZE);
			paintText.getTextBounds(item.getTitle(), 0, item.getTitle().length(), rect);

			rect.inset(-TITLE_MARGIN, -TITLE_MARGIN);
			rect.offsetTo(markerBottomCenterCoords.x - rect.width() / 2,
				markerBottomCenterCoords.y - markerHeight - rect.height());

				paintText.setTextAlign(Paint.Align.CENTER);
				paintText.setTextSize(FONT_SIZE);
				paintText.setARGB(255, 255, 255, 255);
				paintRect.setARGB(130, 0, 0, 0);

				canvas.drawRoundRect(new RectF(rect), 2, 2, paintRect);
				canvas.drawText(item.getTitle(), rect.left + rect.width() / 2,
					rect.bottom - TITLE_MARGIN, paintText);
			}
		}
	}


}
